package org.baicaixiaozhan.springbootwebdemo1.util;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

/**
 * DESC: 时间工具类
 *
 * @author : baicaixiaozhan
 * @since : 2021-01-30
 */
public class DateUtils {

    /**
     * {@link LocalDateTime} 转换为 {@link Date}
     *
     * @param localDateTime
     * @return
     */
    public static Date convertToDate(LocalDateTime localDateTime) {
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }

    /**
     * {@link Date} 转换为 {@link LocalDate}
     *
     * @param date
     * @return
     */
    public static LocalDate convertToLocalDate(Date date) {
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
    }

    /**
     * {@link LocalDate} 转换为 {@link Date}
     *
     * @param localDate
     * @return
     */
    public static Date convertToDate(LocalDate localDate) {
        return Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
    }

    /**
     * {@link Date} 转换为 {@link LocalDateTime}
     *
     * @param date
     * @return
     */
    public static LocalDateTime convertToLocalDateTime(Date date) {
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
    }
}
